﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using pos.entity;
using NHibernate.Criterion;


namespace WebService
{
    /// <summary>
    /// commodity 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class commodity : System.Web.Services.WebService
    {

        public struct TCommodity
        {
            public string id;
            public string name;
            public string price;
            public string amount;
        }
        [WebMethod]
        public TCommodity[] KeyList(string id)
        {
            id = id.Trim();
          //  HttpContext hc=HttpContext.Current;
            
            ProjectionList pl = Projections.ProjectionList()
              .Add(Projections.SqlProjection("sum(saleAmount) as b", new String[] { "b" }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Double }))
              .Add(Projections.GroupProperty("commodityID"));

            t_commodity t = new t_commodity();

            IList list;
            try
            {
                Convert.ToUInt64(id);
                list = t.List(t.DC.Add(Restrictions.Like("commodityID", "%" + id)).AddOrder(Order.Asc("commodityName")));
            }
            catch
            {
                list = t.List(t.DC.Add(Restrictions.Like("commodityKey", "%" + id + "%")).AddOrder(Order.Asc("commodityName")));
            }
           

            //   IList list = t.List(t.DC);
            TCommodity[] rs = new TCommodity[list.Count];
            
            t_sale sale = new t_sale();

            string xid = "";
            IList listx;
            int i = 0;
            
            foreach (Commodity c in list)
            {
                TCommodity x = new TCommodity();
                x.id = c.commodityID;
                x.name = c.commodityName;
                x.price = c.retailPrice.ToString("0.00");
                x.amount = "#";
                if (i > 9)
                {
                    rs[i++] = x;
                    continue;
                }


                AbstractCriterion cri;

                if (c.packageCommodityID == "")
                {
                    //set = x.query(string.Format("where commodityID='{0}' or packageCommodityID='{0}'", txtCmd.Text), "");
                  //  xid = id;
                    xid = x.id;
                    cri = Restrictions.Or(Restrictions.Eq("commodityID", x.id), Restrictions.Eq("packageCommodityID", x.id));
                    listx = t.List(t.DC.Add(cri));
                }
                else
                {
                    //set = x.query(string.Format("where commodityID='{0}' or packageCommodityID='{0}'", x.packageCommodityID), "");
                    cri = Restrictions.Or(Restrictions.Eq("commodityID", c.packageCommodityID), Restrictions.Eq("packageCommodityID", c.packageCommodityID));
                    listx = t.List(t.DC.Add(cri));

                    xid = c.packageCommodityID;
                    //id = x.packageCommodityID; //注释掉，就显示要查询的商品
                }
              
                Hashtable pack = new Hashtable(listx.Count);
                cri = null;
                foreach (Commodity ccc in listx)
                {
                    if (ccc.packageFlag == t_commodity.YESPACKAGE)
                        pack[ccc.packageCommodityID] = ccc.packageAmount;
                    else
                        pack[ccc.commodityID] = 1;
                    if (cri == null)
                        cri = Restrictions.Eq("commodityID", c.commodityID);
                    else
                        cri = Restrictions.Or(cri, Restrictions.Eq("commodityID", c.commodityID));

                }
                list = sale.List(sale.DC.Add(cri).SetProjection(pl));
                string commodityID;

                double amount = 0;
                foreach (Object[] row in list)
                {
                    commodityID = row[1].ToString();
                    amount += Convert.ToDouble(row[0]) * Convert.ToDouble(pack[commodityID]);
                }
                t_storage storage=t_storage.GetByCommodityID(xid);
                if(storage==null)
                    x.amount = (-amount).ToString("0");
                else
                 x.amount =(storage.storageAmount-amount).ToString("0");

                


                //rs.Insert(0, c.commodityName + "," + c.commodityID + "," + c.retailPrice.ToString("0.00"));
                rs[i++] = x;
            }
            return rs;
            //return rs;




        }
    }
}
